//
//  ComposeToolBar.swift
//  weiboSwift1.0
//
//  Created by xuNick on 16/4/27.
//  Copyright © 2016年 NN. All rights reserved.
//

import UIKit

enum ComposeToolBarButtonType: Int {
    case Picture = 0, Mention, Trend, Emoticon, Add
}

class ComposeToolBar: UIStackView {
    
    var clickButtonClosure:((type:ComposeToolBarButtonType) -> ())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    //MARK: - 
    private func setupUI(){
        // 设置子控件相等并且填充父控件
        distribution = .FillEqually
        let view = UIView()
        view.backgroundColor = UIColor.redColor()
        addChildButton("compose_toolbar_picture", type: .Picture)
        addChildButton("compose_mentionbutton_background", type: .Mention)
        addChildButton("compose_trendbutton_background", type: .Trend)
        addChildButton("compose_emoticonbutton_background", type: .Emoticon)
        addChildButton("compose_add_background", type: .Add)

    }
    
    
    private func addChildButton(imgNamed: String,type: ComposeToolBarButtonType) {
        let button = UIButton()
        
        button.addTarget(self, action: "childButtonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        // 取到枚举的原始值
        button.tag = type.rawValue
        // 设置不同状态的图片
        button.setImage(UIImage(named: imgNamed), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "\(imgNamed)_highlighted"), forState: UIControlState.Highlighted)
        // 设置不同状态的背景图片
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Highlighted)
        
        addArrangedSubview(button)
    }
    
    @objc private func childButtonClick(button: UIButton) {
        // 要在此回调，就需要在此执行闭包
        if button.tag == 3 {
            let i = button.imageForState(UIControlState.Normal) == UIImage(named: "compose_keyboardbutton_background") ? 1 : 0
            
            button.setImage(i == 1 ? UIImage(named: "compose_emoticonbutton_background") : UIImage(named: "compose_keyboardbutton_background"), forState: UIControlState.Normal)
        }
        
        
        
        
        clickButtonClosure?(type: ComposeToolBarButtonType(rawValue: button.tag)!)
    }


    
    //MARK: - 懒加载控件
    //
    

}
